package com.example.springboot.demo.controller;

import com.example.springboot.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("r1")
    public String r1(String name) {
        return "r1 " + name;
    }
    //age可以不传参，默认值为0
    @RequestMapping("r2")
    public String r2(UserInfo userInfo) {
        return "r2 " + userInfo.toString();
    }
    //必须传参age
    @RequestMapping("r3")
    public String r3(int age) {
        return "r3 " + age;
    }
    //可以不传参age
    @RequestMapping("r4")
    public String r4(Integer age) {
        return "r4 " + age;
    }
    //将i重命名为age，必传参数
    @RequestMapping("r5")
    public String r5(@RequestParam("i") Integer age) {
        return "r5 " + age;
    }
    //将i重命名为age，非必传参数
    @RequestMapping("r6")
    public String r6(@RequestParam(value = "i",required = false) Integer age) {
        return "r6 " + age;
    }
    //传递数组参数
    @RequestMapping("r7")
    public String r7(String[] ages) {
        return "r7 " + Arrays.toString(ages);
    }
    //传递集合参数
    @RequestMapping("r8")
    public String r8(@RequestParam List<Integer> ages) {
        return "r8 " + ages.toString();
    }
    //传递JSON参数
    @RequestMapping("r9")
    public String r9(@RequestBody UserInfo userInfo) {
        return "r9 " + userInfo.toString();
    }
    //从url中获取参数
    @RequestMapping("r10/{articleId}")
    public String r10(@PathVariable Integer articleId) {
        return "文章ID" + articleId;
    }
    //上传文件
    @RequestMapping("r11")
    public String r11(@RequestParam("file") MultipartFile file) {

        return "r11 " + file.getOriginalFilename();
    }
    //Cookie获取
    @RequestMapping("r12")
    public String r12(@CookieValue("JSESSIONID") String sessionId) {
        return "r12 " + sessionId;
    }
    //Cookie获取
    @RequestMapping("r13")
    public String r13(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }
        return "获取 Cookie";
    }
    //存储session
    @RequestMapping("r14")
    public String setSession(HttpServletRequest request){
        //从cookie中获取sessionId,根据sessionId获取session对象
        HttpSession session = request.getSession();
        //设置session
        session.setAttribute("sessionId",12);
        session.setAttribute("sessionName","zhangsan");
        return "设置session成功";
    }
    //获取session
    @RequestMapping("r15")
    public String getSession(HttpServletRequest request){
        //从cookie中获取sessionId,根据sessionId获取session对象
        HttpSession session = request.getSession(false);
        if(session == null){
            return "用户未登录";
        }else{
            String sessionId =(String) session.getAttribute("sessionId");
            return sessionId;
        }
    }
    //获取session
    @RequestMapping("r16")
    public String getSession2(HttpSession session){
            String sessionId =(String) session.getAttribute("sessionId");
            return sessionId;
    }
    //获取session
    @RequestMapping("r17")
    public String getSession3(@SessionAttribute("sessionId")String sessionId){
        return sessionId;
    }
    //获取header
    @RequestMapping("r18")
    public String getHeader(HttpServletRequest request){
        String header = request.getHeader("user-agent");
        return "获取header:"+header;
    }
    //获取header
    @RequestMapping("r19")
    public String getHeader2(@RequestHeader("user-agent")String header){
        return "获取header:"+header;
    }
}
